<template>
  <RouterView />
</template>

<script setup>
import { RouterView, useRoute } from 'vue-router'
import { watch, onMounted } from 'vue'
import { getVisitorId } from '@/utils/visitor'
import { recordPageVisit } from '@/api'

const route = useRoute()

// 记录页面访问
const recordVisit = async () => {
  try {
    const visitorId = getVisitorId()
    await recordPageVisit({
      visitorId,
      pageUrl: window.location.href,
      referer: document.referrer || ''
    })
  } catch (error) {
    // 静默处理错误，不影响用户体验
    console.debug('记录访问失败:', error)
  }
}

// 首次加载时记录访问
onMounted(() => {
  recordVisit()
})

// 路由变化时记录访问
watch(
  () => route.path,
  () => {
    recordVisit()
  }
)
</script>
